local js_rangjie = fk.CreateSkill {
  name = "js__rangjie",
}

js_rangjie:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and #player.room:canMoveCardInBoard() > 0
  end,
  on_trigger = function(self, event, target, player, data)
    local ret
    for i = 1, data.damage do
      ret = self:doCost(event, target, player, data)
      if ret then return ret end
    end
  end,
  on_cost =function (self, event, target, player, data)
    local room = player.room
    local targets = room:askToChooseToMoveCardInBoard(player,{prompt="#js__rangjie-move",skill_name=self.name,cancelable=true})
    if #targets > 0 then
      event:setCostData(self,{tos=targets})
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos
    local result = room:askToMoveCardInBoard(player,{target_one=targets[1],target_two=targets[2],skill_name=self.name})
    if player.dead then return end
    if result==nil then return end
    local suit = result.card:getSuitString(true)
    local cards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.discard_pile, info.cardId) and Fk:getCardById(info.cardId):getSuitString(true) == suit then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    if #cards > 0 then
      cards = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "#js__rangjie-prey:::"..suit, {"Cancel"})
      if #cards > 0 then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player)
      end
    end
  end,
})

return js_rangjie